<?php

defined('SYSPATH') or die('No direct access allowed.');

class Controller_Admin extends Controller_Site {

    public function action_index() {
        $this->request->redirect('admin/tasks');
    }

    public function action_tasks() {
        $worlds = ORM::factory('world')->find_all();
        $addmediaView = View::factory('admin/addmedia')->set('worlds', $worlds);
        $this->content->set('addmediaView', $addmediaView);

        $addworldView = View::factory('admin/addworld');
        $this->content->set('addworldView', $addworldView);

        $addauthorView = View::factory('admin/addauthor');
        $this->content->set('addauthorView', $addauthorView);
    }

    public function action_addmedia() {
        $worlds = ORM::factory('world')->find_all();
        $this->content->set('worlds', $worlds);
        if (($this->request->method() == 'POST')) {
            $media = ORM::factory('books');
            $media->title = $this->request->post('title');
            $media->release_date = $this->request->post('date');
            $media->world_id = $this->request->post('world');
            $media->quantity = $this->request->post('db');
            if (($this->request->post('novel')) != '') {
                $media->ISBN = $this->request->post('isbn');
                $media->series = $this->request->post('series');
                $media->save();
            }
            if (($this->request->post('magazine')) != '') {
                $magazine = ORM::factory('magazine');
                $media->save();
                $magazine->media_id = $media->id;
                $magazine->year = $this->request->post('year');
                $magazine->number = $this->request->post('number');
                $magazine->save();
            }
            if (($this->request->post('rulebook')) != '') {
                $media->ISBN = $this->request->post('isbn');
                $media->series = $this->request->post('series');
                $media->save();
            }
            $this->request->redirect('admin/tasks');
        }
    }

    public function action_addworld() {
        if (($this->request->method() == 'POST')) {
            $world = DB::select()->from('world')->where(DB::expr('LOWER(name)'), '=', strtolower($this->request->post('world_name')))->execute();
            if ($world->count() > 0) {
                Session::instance()->set('world_error', 'Már létező világ!');
            } else {
                $world = ORM::factory('world');
                $world->name = $this->request->post('world_name');
                $world->save();
            }
            $this->request->redirect('admin/tasks');
        }
    }

    public function action_addauthor() {
        if (($this->request->method() == 'POST')) {
            $author = ORM::factory('author')->where('second_name', '=', $this->request->post('second_name'))->where('first_name', '=', $this->request->post('first_name'))->find();
            
            if ($author->loaded()) {
                Session::instance()->set('author_error', 'Már létezik ilyen nevű szerző!');
            } else {
                $author = ORM::factory('author');
                $author->second_name = $this->request->post('second_name');
                $author->first_name = $this->request->post('first_name');
                if ($this->request->post('second_name_first') != NULL) {
                    $author->second_name_first = 1;
                }
                else{
                    $author->second_name_first = 0;
                }
                $author->save();
            }
            $this->request->redirect('admin/tasks');
        }
    }
    
    public function action_suspendloan(){
        
    }

}